package gui.calendarview;

import gui.ActivityWindow;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import no.ntnu.fp.model.ActivityModel;
import no.ntnu.fp.model.Person;

public class WeekViewPanel extends CalendarView implements ListSelectionListener {

	private static final long serialVersionUID = 1L;
	private DayListComponent[] days;
	private JLabel week;
	
	public WeekViewPanel(Date date, Person user) {
		super(date,user);
		JPanel weekPanel = new JPanel();
		week = new JLabel("Uke " + ct.getWeekOfYear(date));
		updateList();
		
		GridBagLayout gbl = new GridBagLayout();
		int dw = 137;
		gbl.columnWidths = new int[] { 41,dw,dw,dw,dw,dw,dw,dw };
		gbl.rowHeights = new int[] { 20,450 };
		weekPanel.setLayout(gbl);
		GridBagConstraints c = new GridBagConstraints();
		addComponentToLayout(weekPanel, week,c,0,0,1,1, GridBagConstraints.CENTER);
		addComponentToLayout(weekPanel, days[0],c,0,1,1,1, GridBagConstraints.BOTH);
		String[] dayNames = ct.getDayNames();
		for (int i = 0; i < 14; i++) {
			if ( i < 7 ) {
				JLabel dayName = new JLabel(dayNames[i].substring(0, 1).toUpperCase() + dayNames[i].substring(1));
				addComponentToLayout(weekPanel, dayName,c,i+1,0,1,1, GridBagConstraints.CENTER);
			} else {
				addComponentToLayout(weekPanel, days[i-6],c,i-6,1,1,1, GridBagConstraints.BOTH);
			}
		}
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(new JLabel("Ukesvisning"));
		add(weekPanel);
	}
	
	private void addComponentToLayout(JPanel panel, JComponent component, GridBagConstraints c, int x, int y, int height, int width, int fill) {
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = width;
		c.gridheight = height;
		c.fill = fill;
		panel.add(component, c);
	}
	
	public void setCurrentDateShowing(Date date) {
		currentDateShowing = date;
		for (int i = 0; i < days.length; i++) {
			days[i].setDay(date);
		}
	}
	
	public void updateList() {
		if ( days == null ) {
			days = new DayListComponent[8];
			Date date = ct.getFirstDayOfWeek(currentDateShowing);
			days[0] = new DayListComponent(date,true,user);
			days[0].setSelectionBackground(days[0].getBackground());
			days[0].setSelectionForeground(days[0].getForeground());
			for (int i = 1; i < days.length; i++) {
				days[i] = new DayListComponent(date,false,user);
				days[i].addListSelectionListener(this);
				date = ct.getNextDay(date);
			}
		}
		for ( DayListComponent d : days ) {
			d.updateList();
		}
	}

	public void valueChanged(ListSelectionEvent e) {
		for (int i = 0; i < days.length; i++) {
			if ( !e.getSource().equals(days[i]) ) {
				days[i].clearSelection();
			} else {
				Object o = ((JList)days[i]).getSelectedValue();
				if ( o != null ) {
					ActivityModel model = ((ListElement)o).getModel();
					if ( model != null )
						if ( isAllowedToOpenWindow ) {
							new ActivityWindow(model);
							updateList();
							isAllowedToOpenWindow = false;
						} else {
							isAllowedToOpenWindow = true;
						}
				}
			}
		}
	}

}
